package com.wmm.jhdriver;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.common.dialog.MyDialog;
import com.common.net.mywidget.LoadingDialog;
import com.common.util.SPUtils;
import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

public class HistoryAdapter extends BaseAdapter {
    private Context context;
    private List<Bean> list;
    private LayoutInflater inflater;
    private FragmentManager manager;
    LoadingDialog dialog;

    public HistoryAdapter(Context context, List<Bean> list, FragmentManager manager) {
        this.context = context;
        this.list = list;
        dialog=new LoadingDialog(context);
        this.manager=manager;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Hodler hodler;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_his, null);
            hodler = new Hodler();
            hodler.button = convertView.findViewById(R.id.button);
            hodler.time = convertView.findViewById(R.id.time);
            hodler.type = convertView.findViewById(R.id.type);
            convertView.setTag(hodler);
        } else {
            hodler = (Hodler) convertView.getTag();
        }
        final Bean bean = list.get(position);
        switch (bean.type) {
            case "1":
                hodler.type.setText("落地签");
                break;
            case "2":
                hodler.type.setText("回厂签");
                break;
        }
        hodler.time.setText(bean.time);
        hodler.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONArray jsonArray=null;
                String temp="";
                final List<GangPingBean> gangPingBeanList=new ArrayList<>();
                try {
                    jsonArray=new JSONArray(bean.list);
                    if(jsonArray!=null){
                        for (int i=0;i<jsonArray.length();i++){
                            gangPingBeanList.add(new Gson().fromJson(jsonArray.getJSONObject(i).toString(),GangPingBean.class));
                            temp+=jsonArray.getJSONObject(i).getString("GangPingHao");
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                MyDialog.showMaterialDialog(manager, bean.ydhao+" "+bean.kehu, temp, "删除记录", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MyApp.getInstance().delete(SQLiteDbHelper.TABLE_STUDENT,
                                "time = ?", new String[]{bean.time});
                        list.remove(position);
                        notifyDataSetChanged();
                    }
                }, "重新提交", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        submit(gangPingBeanList,bean.ydhao,bean.type,position,bean.time);
                    }
                }).show();
            }
        });
        return convertView;
    }
    private void submit(final List<GangPingBean> list, String ydhao, String type, final int position, final String time){
        JSONArray array=null;
        try {
            array=new JSONArray();
            JSONObject object;
            for(int i=0;i<list.size();i++){
                object=new JSONObject();
                object.put("gangpinghao",list.get(i).getGangPingHao());
                array.put(object);
            }
            object=new JSONObject();
            object.put("sFaYunDanHao",ydhao);
            object.put("sYu",SPUtils.get(context,"YuInfo","").toString());
            object.put("sYunshufangshi","1".equals(type)?"0":"1");
            object.put("GangPingHao",array.toString().replace("=",":"));
            RequestParams requestParams=new RequestParams();
            requestParams.put("sJson",object.toString().replace("=",":"));
            HttpUtils.post(Constant.DRIVER_LUODI_SUBMIT, requestParams, new TextHttpResponseHandler() {
                @Override
                public void onStart() {
                    super.onStart();
                    dialog.show();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    dialog.dismiss();
                }
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    try {
                        JSONObject object1=new JSONObject(responseString);
                        if(object1.getInt("success")==0){
                            MyApp.getInstance().delete(SQLiteDbHelper.TABLE_STUDENT,
                                    "time = ?", new String[]{time});
                            list.remove(position);
                            notifyDataSetChanged();
                            T.showShort(context,"提交成功");
                        }else{
                            T.showShort(context,object1.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    class Hodler {
        TextView type;
        TextView time;
        Button button;
    }
}
